package com.nt.oam.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nt.common.core.domain.entity.SysRole;
import com.nt.common.core.domain.entity.SysUser;
import com.nt.common.enums.YesOrNoEnum;
import com.nt.common.utils.DateUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.nt.common.utils.SecurityUtils;
import com.nt.common.utils.StringUtils;
import com.nt.common.utils.bean.BeanUtils;
import com.nt.oam.domain.*;
import com.nt.oam.vo.asset.HardwareAssetVo;
import com.nt.system.service.impl.SysRoleServiceImpl;
import com.nt.system.service.impl.SysUserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nt.oam.mapper.AntHardwareAssetMapper;
import com.nt.oam.service.IAntHardwareAssetService;

/**
 * 硬件资产Service业务层处理
 *
 * @author JZFeng
 * @date 2025-02-05
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AntHardwareAssetServiceImpl extends ServiceImpl<AntHardwareAssetMapper, AntHardwareAsset> implements IAntHardwareAssetService {
    private final AntHardwareAssetMapper antHardwareAssetMapper;
    private final SysRoleServiceImpl sysRoleService;
    private final AntHospitalServiceImpl antHospitalService;
    private final AntHospitalProjectServiceImpl antHospitalProjectService;
    private final AntAssetCorrelationServiceImpl antAssetCorrelationService;
    private final SysUserServiceImpl sysUserService;

    /**
     * 查询硬件资产
     *
     * @param hardwareAssetId 硬件资产主键
     * @return 硬件资产
     */
    @Override
    public AntHardwareAsset selectAntHardwareAssetByHardwareAssetId(Long hardwareAssetId) {
        return antHardwareAssetMapper.selectAntHardwareAssetByHardwareAssetId(hardwareAssetId);
    }

    /**
     * 查询硬件资产列表
     *
     * @param antHardwareAsset 硬件资产
     * @return 硬件资产
     */
    @Override
    public List<AntHardwareAsset> selectAntHardwareAssetList(AntHardwareAsset antHardwareAsset) {
        return antHardwareAssetMapper.selectAntHardwareAssetList(antHardwareAsset);
    }

    /**
     * 新增硬件资产
     *
     * @param antHardwareAsset 硬件资产
     * @return 结果
     */
    @Override
    public int insertAntHardwareAsset(AntHardwareAsset antHardwareAsset) {
        antHardwareAsset.setCreateTime(DateUtils.getNowDate());
        // 医院的id
        Long hospitalId = antHardwareAsset.getHospitalId();
        antHardwareAsset.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antHardwareAsset.setHospitalName(antHospital.getHospitalName());
        }
        // 项目id
        Long projectId = antHardwareAsset.getProjectId();
        antHardwareAsset.setProjectId(projectId);
        AntHospitalProject antHospitalProject = antHospitalProjectService.getById(projectId);
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antHardwareAsset.setProjectName(antHospitalProject.getProjectName());
        }
        // 创建人
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antHardwareAsset.setCreatePerson(sysUser.getUserName());
        }
        antHardwareAsset.setIsDeleted(YesOrNoEnum.NO.getCode());
        antHardwareAsset.setIsModify(YesOrNoEnum.NO.getCode());
        return antHardwareAssetMapper.insertAntHardwareAsset(antHardwareAsset);
    }

    /**
     * 修改硬件资产
     *
     * @param antHardwareAsset 硬件资产
     * @return 结果
     */
    @Override
    public int updateAntHardwareAsset(AntHardwareAsset antHardwareAsset) {
        antHardwareAsset.setUpdateTime(DateUtils.getNowDate());
        // 医院的id
        Long hospitalId = antHardwareAsset.getHospitalId();
        antHardwareAsset.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antHardwareAsset.setHospitalName(antHospital.getHospitalName());
        }
        // 项目id
        Long projectId = antHardwareAsset.getProjectId();
        antHardwareAsset.setProjectId(projectId);
        AntHospitalProject antHospitalProject = antHospitalProjectService.getById(projectId);
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antHardwareAsset.setProjectName(antHospitalProject.getProjectName());
        }
        // 更新人
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antHardwareAsset.setUpdatePerson(sysUser.getUserName());
        }
        return antHardwareAssetMapper.updateAntHardwareAsset(antHardwareAsset);
    }

    /**
     * 批量删除硬件资产
     *
     * @param hardwareAssetIds 需要删除的硬件资产主键
     * @return 结果
     */
    @Override
    public int deleteAntHardwareAssetByHardwareAssetIds(Long[] hardwareAssetIds) {
        List<Long> hardwareAssetIdsList = Arrays.asList(hardwareAssetIds);
        hardwareAssetIdsList.forEach(a -> {
            AntHardwareAsset hardwareAsset = antHardwareAssetMapper.selectAntHardwareAssetByHardwareAssetId(a);
            hardwareAsset.setIsDeleted(YesOrNoEnum.YES.getCode());
            antHardwareAssetMapper.updateAntHardwareAsset(hardwareAsset);
        });
        //antHardwareAssetMapper.deleteAntHardwareAssetByHardwareAssetIds(hardwareAssetIds);
        return YesOrNoEnum.YES.getCode();
    }

    /**
     * 删除硬件资产信息
     *
     * @param hardwareAssetId 硬件资产主键
     * @return 结果
     */
    @Override
    public int deleteAntHardwareAssetByHardwareAssetId(Long hardwareAssetId) {
        return antHardwareAssetMapper.deleteAntHardwareAssetByHardwareAssetId(hardwareAssetId);
    }

    /**
     * 查询硬件资产详情信息
     *
     * @param hardwareAssetId 硬件资产主键
     * @return 硬件资产
     */
    public HardwareAssetVo selectHardwareAssetMessage(Long hardwareAssetId) {
        // 源对象
        AntHardwareAsset antHardwareAsset = antHardwareAssetMapper.selectAntHardwareAssetByHardwareAssetId(hardwareAssetId);
        //医院名称
        AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antHardwareAsset.getHospitalId());
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antHardwareAsset.setHospitalName(antHospital.getHospitalName());
        }
        // 项目名称
        AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antHardwareAsset.getProjectId());
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antHardwareAsset.setProjectName(antHospitalProject.getProjectName());
        }
        // 资产名称
        AntAssetCorrelation antAssetCorrelation = antAssetCorrelationService.selectAntAssetCorrelationByAssetId(antHardwareAsset.getAssetId());
        if (Objects.nonNull(antAssetCorrelation) && StringUtils.isNotEmpty(antAssetCorrelation.getAssetName())) {
            antHardwareAsset.setAssetName(antAssetCorrelation.getAssetName());
        }
        // 目标对象
        HardwareAssetVo hardwareAssetVo = new HardwareAssetVo();
        BeanUtils.copyProperties(antHardwareAsset, hardwareAssetVo);
        return hardwareAssetVo;
    }

    /**
     * 通过项目的id来查询硬件资产详情信息
     *
     * @return 硬件资产
     */
    public List<HardwareAssetVo> selectByProjectIdHardwareAsset(Long projectId) {
        List<HardwareAssetVo> hardwareAssetVoList = new ArrayList<>();
        // 源对象
        List<AntHardwareAsset> antHardwareAssetList = antHardwareAssetMapper.selectByProjectIdHardwareAsset(projectId);
        if (Objects.nonNull(antHardwareAssetList) && antHardwareAssetList.size() > 0) {
            antHardwareAssetList.forEach(antHardwareAsset -> {
                //医院名称
                AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antHardwareAsset.getHospitalId());
                if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
                    antHardwareAsset.setHospitalName(antHospital.getHospitalName());
                }
                // 项目名称
                AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antHardwareAsset.getProjectId());
                if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
                    antHardwareAsset.setProjectName(antHospitalProject.getProjectName());
                }
                // 目标对象
                HardwareAssetVo hardwareAssetVo = new HardwareAssetVo();
                BeanUtils.copyProperties(antHardwareAsset, hardwareAssetVo);
                hardwareAssetVoList.add(hardwareAssetVo);
            });
        }
        return hardwareAssetVoList;
    }


    /**
     * 通过医院的id来查询硬件资产详情信息
     *
     * @return 硬件资产
     */
    public List<HardwareAssetVo> selectByHospitalIdHardwareAsset(Long hospitalId) {
        List<HardwareAssetVo> hardwareAssetVoList = new ArrayList<>();
        // 源对象
        List<AntHardwareAsset> antHardwareAssetList = antHardwareAssetMapper.selectByHospitalIdHardwareAsset(hospitalId);
        if (Objects.nonNull(antHardwareAssetList) && antHardwareAssetList.size() > 0) {
            antHardwareAssetList.forEach(antHardwareAsset -> {
                //医院名称
                AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antHardwareAsset.getHospitalId());
                if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
                    antHardwareAsset.setHospitalName(antHospital.getHospitalName());
                }
                // 项目名称
                AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antHardwareAsset.getProjectId());
                if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
                    antHardwareAsset.setProjectName(antHospitalProject.getProjectName());
                }
                // 目标对象
                HardwareAssetVo hardwareAssetVo = new HardwareAssetVo();
                BeanUtils.copyProperties(antHardwareAsset, hardwareAssetVo);
                hardwareAssetVoList.add(hardwareAssetVo);
            });
        }
        return hardwareAssetVoList;
    }

    /**
     * 查询是否在修改
     */
    public int selectHardwareModify(Long hardwareAssetId) {
        AntHardwareAsset antHardwareAsset = this.getById(hardwareAssetId);
        int isModify = 0;
        if (Objects.nonNull(antHardwareAsset) && Objects.nonNull(antHardwareAsset.getIsModify())) {
            isModify = antHardwareAsset.getIsModify();
        }
        return isModify;
    }

    /**
     * 重新设置修改状态
     */
    public int updateHardwareModify(Long hardwareAssetId, Integer isModify) {
        AntHardwareAsset antHardwareAsset = this.getById(hardwareAssetId);
        if (Objects.nonNull(antHardwareAsset)) {
            antHardwareAsset.setIsModify(isModify);
        }
        return antHardwareAssetMapper.updateAntHardwareAsset(antHardwareAsset);
    }
}
